package 力扣._00周赛._424周赛.问题3;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

class Solution2 {
    public int minZeroArray(int[] nums, int[][] queries) {
        int n = nums.length;

        int sum = 0;
        for(int num:nums)
            sum+=num;
        if(sum==0)
            return 0;
        // hashmap存储大于0的值
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < n; i++) {
            if(nums[i]>0)
                map.put(i, nums[i]);
        }

        for (int k = 0; k < queries.length; k++) {
            int l = queries[k][0];
            int r = queries[k][1];
            int val = queries[k][2];

            Set<Integer> keySet = map.keySet();
            for (Integer i : keySet) {
                if(i>=l && i<=r){
                    map.put(i, map.get(i)-val);
                    if (map.get(i)<=0 )
                        map.remove(i);
                }
            }

            if(map.isEmpty())
                return k+1;
        }

        return -1;
    }
}